package com.xiaohui.gwc.controller;

import com.xiaohui.gwc.mapper.JwtUserMapper;
import com.xiaohui.gwc.mode.JwtUser;
import com.xiaohui.gwc.mode.Result;
import com.xiaohui.gwc.mode.ShoppingC;
import com.xiaohui.gwc.mode.ShoppingCart_Update;
import com.xiaohui.gwc.service.Shopping_cartService;
import com.xiaohui.gwc.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Poppin
 * @Date: 2021/11/30 13:42
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/cart")
public class ShoppingCartController {

    Shopping_cartService shopping_cartService;
    @Autowired
    public void setShopping_cartService(Shopping_cartService shopping_cartService){
        this.shopping_cartService = shopping_cartService;
    }
    JwtUserMapper jwtUserMapper;
    @Autowired
    public void setJwtUserMapper(JwtUserMapper jwtUserMapper){
        this.jwtUserMapper = jwtUserMapper;
    }

    /**
     * 查询购物车
     * @param Htoken
     * @return
     */
    @GetMapping("/query")
    public Result getAll(@RequestHeader("Authorization")String Htoken){
        String token = Htoken.replace(JwtUtil.TOKEN_PREFIX,"");
        JwtUser jwtUser = jwtUserMapper.loadUserByUsername(JwtUtil.getUsername(token));
        return shopping_cartService.QueryShoppingCart(jwtUser.getUserId());
    }
    @PostMapping("/update")
    public Result Update(@RequestHeader("Authorization")String Htoken, @RequestBody ShoppingCart_Update shoppingCart_update){
        String token = Htoken.replace(JwtUtil.TOKEN_PREFIX,"");
        JwtUser jwtUser = jwtUserMapper.loadUserByUsername(JwtUtil.getUsername(token));
        return shopping_cartService.updateShoppingCart(shoppingCart_update,jwtUser.getUserId());
    }
    @GetMapping("/delete")
    public Result delete(@RequestHeader("Authorization")String Htoken,@RequestParam("id")String id){
        String token = Htoken.replace(JwtUtil.TOKEN_PREFIX,"");
        JwtUser jwtUser = jwtUserMapper.loadUserByUsername(JwtUtil.getUsername(token));
        return shopping_cartService.deleteShoppingCart(id, jwtUser.getUserId());
    }
    @PostMapping("/add")
    public Result add(@RequestHeader("Authorization")String Htoken, @RequestBody ShoppingC shoppingC){
        String token = Htoken.replace(JwtUtil.TOKEN_PREFIX,"");
        JwtUser jwtUser = jwtUserMapper.loadUserByUsername(JwtUtil.getUsername(token));
        log.info("用户ID:"+jwtUser.getUserId() + ":"+shoppingC.getProd_id());
        return shopping_cartService.addShoppingCart(shoppingC, jwtUser.getUserId());
    }
    @GetMapping("/recomn")
    public Result Reco(@RequestHeader("Authorization")String Htoken){
        String token = Htoken.replace(JwtUtil.TOKEN_PREFIX,"");
        JwtUser jwtUser = jwtUserMapper.loadUserByUsername(JwtUtil.getUsername(token));
        return shopping_cartService.Recomne(jwtUser.getUserId());
    }


}
